package xx.iverson.navigation_manager.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import xx.iverson.navigation_manager.entity.NavCategory;
import xx.iverson.navigation_manager.entity.Navigation;
import xx.iverson.navigation_manager.entity.SiteConfig;
import xx.iverson.navigation_manager.service.NavCategoryService;
import xx.iverson.navigation_manager.service.NavigationService;
import xx.iverson.navigation_manager.service.SiteConfigService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * xx.iverson.navigation_manager.controller.IndexController
 * 描述：
 * on 2025/9/11-下午4:28
 *
 * @author weijx
 */


@Controller
public class IndexController {

    @Autowired
    private NavCategoryService categoryService;

    @Autowired
    private NavigationService navigationService;

    @Autowired
    private SiteConfigService siteConfigService;

    @GetMapping("/")
    public String index(Model model) {
        // 获取网站标题
        SiteConfig siteConfig = siteConfigService.getSiteConfig();
        model.addAttribute("siteTitle", siteConfig != null ? siteConfig.getSiteTitle() : "网站导航");

        // 获取所有分类
        List<NavCategory> categories = categoryService.findAll();
        model.addAttribute("categories", categories);

        // 按分类获取导航链接
        Map<Integer, List<Navigation>> navMapAndImage = new HashMap<>();
        Map<Integer, List<Navigation>> navMapNoImage = new HashMap<>();
        for (NavCategory category : categories) {
            List<Navigation> navigations = navigationService.findByCategoryId(category.getId());
            navMapAndImage.put(category.getId(), navigations);
            List<Navigation> navigationsNoImage = navigationService.findByCategoryIdNoImage(category.getId());
            navMapNoImage.put(category.getId(), navigationsNoImage);
        }
        model.addAttribute("navMapAndImage", navMapAndImage);
        model.addAttribute("navMapNoImage", navMapNoImage);

        return "index";
    }
}
